<template>
  <!--会员费用设置组件 -->
  <div class="change_admin">
    <el-dialog
      title="新增"
      class="gl_dialog_TitleLine"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      width="500px"
    >
      <el-form ref="form" class="gl_form_mini" :model="vo" :rules="FeeSettingsVO_Rules" label-width="80px" label-position="right" style="padding-right: 30px;">
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="费用名称" prop="name">
              <el-input v-model="vo.name" size="mini" clearable />
            </el-form-item>
            <el-row v-if="feeType === 'member'" type="flex" align="middle">
              <el-col :span="11">
                <el-form-item label="年限范围" prop="minYear">
                  <el-input-number v-model="vo.minYear" :min="0" size="mini" :controls="false" clearable />
                </el-form-item>
              </el-col>
              <el-col :span="1" align="center" style="margin-top: -20px;margin-left: 40px;">——</el-col>
              <el-col :span="11" style="margin-left: -40px;">
                <el-form-item prop="maxYear">
                  <el-input-number v-model="vo.maxYear" :min="0" size="mini" :controls="false" clearable />
                </el-form-item>
              </el-col>
              <el-col :span="1" style="margin-top: -20px;margin-left: 20px;">年</el-col>
            </el-row>
            <el-form-item label="价格" prop="price">
              <el-input-number v-model="vo.price" :precision="2" :min="0" size="mini" :controls="false" clearable />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="close()">取消</el-button>
        <el-button type="primary" size="mini" @click="saveNew('form')">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { FeeSettingsVO_Rules, OhterFeeSettingsVO_Rules } from '@/lib/validate/train/expenses/index.js';
import { SiSTKWTrainCreateTrainningMembershipFeeInput, SiSTKWTrainCreateTrainningFeeInput } from '@/lib/form/train/swaggerForm.js';
export default {
  name: 'Fee',
  props: {
    feeType: {
      type: String,
      default() {
        return '';
      }
    }
  },
  data() {
    return {
      vo: new SiSTKWTrainCreateTrainningMembershipFeeInput(),
      FeeSettingsVO_Rules: new FeeSettingsVO_Rules(),
      dialogVisible: false,
      inputDataName: ''// 获取表单框内输入的名称，判断其唯一性
    };
  },
  watch: {
    // 监听表单窗口状态如果关闭则清除提示信息
    dialogVisible(val) {
      if (!val) {
        this.$refs['form'].resetFields();
        if (this.feeType === 'member') {
          this.vo = new SiSTKWTrainCreateTrainningMembershipFeeInput();
        } else {
          this.vo = new SiSTKWTrainCreateTrainningFeeInput();
        }
      }
    }
  },
  created() {
    if (this.feeType === 'other') {
      this.FeeSettingsVO_Rules = new OhterFeeSettingsVO_Rules();
      this.vo = new SiSTKWTrainCreateTrainningFeeInput();
    }
  },
  methods: {
    // 获取数据
    freshData() {
    },
    open(row) {
      this.dialogVisible = true;
    },
    close() {
      this.dialogVisible = false;
    },
    // 点击确定进行添加或者编辑的操作
    saveNew(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var editData = JSON.parse(JSON.stringify(this.vo));
          editData.price = this.yuanToFen(editData.price);
          if (this.feeType === 'other') {
            this.$emit('pushTabelData', editData);
          } else {
            if (this.vo.minYear <= this.vo.maxYear) {
              this.$emit('pushTabelData', editData);
            } else {
              this.$message({
                message: `开始年限不得大于结束年限`,
                type: 'error',
                showClose: true
              });
            }
          }
        }
      });
    },
    // 格式化货币格式 （分）
    formatMoney(num) {
      if (typeof num === 'number' && !Number.isNaN(num)) {
        // num 是数字类型 且不是 NaN
        let strNum = String(num); // 金额文本类型
        if (num >= 0) {
          strNum = strNum.padStart(3, '0'); // 用0填充保证最小三位数
          return strNum.slice(0, -2) + '.' + strNum.slice(-2); // 金额拼接
        } else {
          const _mark = strNum.slice(0, 1); // 负号
          const _noMark = strNum.slice(1); // 无负号数值
          strNum = _noMark.padStart(3, '0'); // 用0填充保证最小三位数
          return _mark + strNum.slice(0, -2) + '.' + strNum.slice(-2); // 金额拼接
        }
      } else {
        return '';
      }
    },
    // 将元转化为分
    yuanToFen(moneyStr) {
      const _moneyStr = String(moneyStr);
      const yuan = Number(_moneyStr.split('.')[0]) || 0;
      const fen = Number(_moneyStr.split('.')[1]) || 0;
      return yuan * 100 + fen;
    }
  }
};
</script>

